<template>
	<view class="order-container c-container wedding-container">
		<navigator class="fixed-right" v-if="order_id" :url="'/pages/order/detail/detail?order_id=' + order_id"><text>查看定单详情</text></navigator>
		<view class="c-row-box">
			<view class="c-row-head"><view class="c-row-title">婚宴信息</view></view>
			<view class="c-row-item">
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">
							<text style="color: red;">*</text>
							承办公司
						</view>
						<view class="col-text">
							<picker mode="selector" :range="companyList" range-key="title" :disabled="isPreview" @change="wedingCompanyChange">
								<view>{{ banquetParams.company_text == '' ? '请选择' : banquetParams.company_text }}</view>
							</picker>
						</view>
					</view>
				</view>
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">
							<text style="color: red;">*</text>
							签约时间
						</view>
						<view class="col-text">
							<picker mode="date" :start="startDate" :end="endDate" @change="signDateChange" :disabled="isPreview">
								<view>{{ banquetParams.sign_date }}</view>
							</picker>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 其他费用列表 -->
		<other-list v-if="addons.length > 0" :addons="addons" :isPreview="isPreview" @deleteOtherItem="deleteOtherItem" @editOtherList="editOtherList"></other-list>

		<!-- 添加内容弹窗 -->
		<add-dialog
			v-if="showDialog"
			ref="addDialog"
			:addons="addons"
			:addType="add_type"
			:ritualList="ritualList"
			:isPreview="isPreview"
			@closeDialog="closeDialog"
			@getOtherPrice="getOtherPrice"
		></add-dialog>

		<!-- 收款信息 -->
		<divider title="收款信息"></divider>
		<income-info ref="income"
			:incomeParams="incomeParams"
			:paymentList="paymentList"
			:startDate="startDate"
			:endDate="endDate"
			:incomeType="incomeParams.news_type"
			:isPreview="isPreview"
		></income-info>

		<!-- 合同信息 -->
		<divider title="合同信息"></divider>
		<contract-info :orderParams="orderParams" :isPreview="isPreview"></contract-info>

		<!-- 底部栏-创建 -->
		<view class="footer-box" v-if="confirm_id == 0">
			<view class="footer-text">
				<text>增项小计：{{ banquetParams.banquet_totals }}</text>
			</view>
			<view class="footer-action-btn">
				<button size="mini" @click="addOther">添加项目</button>
				<button size="mini" @click="saveFn">提交</button>
			</view>
		</view>

		<divider title="审核流程" v-if="sequenceData.length > 0 || confirmList.length > 0 || confirmSteps.length > 0"></divider>
		<!-- 新版审批流程————创建  -->
		<template v-if="sequenceData.length > 0">
			<steps-create :stepList="sequenceData"></steps-create>
		</template>

		<!-- 老版审核流程 -->
		<template v-if="confirmList.length > 0">
			<auditProcess
				:value="confirmList"
				:btnValue="buttons"
				:isShowBtn="options.api ? false : true"
				:transferForm="transferForm"
				@clearTransferForm="clearTransferForm"
				@resubmit="resubmits"
			></auditProcess>
		</template>
		<!-- 新版审核流程————查看 -->
		<template v-if="confirmSteps.length > 0">
			<steps-look :stepList="confirmSteps"></steps-look>
			<footer-action-btn
				:btnValue="buttons"
				:transferForm="transferForm"
				@clearTransferForm="clearTransferForm"
				:isShowBtn="options.api ? false : true"
				@resubmit="resubmits"
				@transferSubmit="transferSubmit"
				@addOther="addOther"
			></footer-action-btn>
		</template>
	</view>
</template>

<script>
import addDialog from '../childComps/addDialog/addDialog.vue';
import otherList from '../childComps/otherList/otherList.vue';
import incomeInfo from '@/pages/order/createOrder/banquet/income.vue';
import contractInfo from '@/pages/order/createOrder/common/note.vue';

import divider from '@/component/divider/divider.vue';
import stepsLook from '@/component/steps-look/steps-look.vue'; // 新版审核流程
import footerActionBtn from '@/component/footer-action-btn/footer-action-btn.vue'; // 底部操作按钮组
import stepsCreate from '@/component/steps-create/steps-create.vue';
import auditProcess from '@/component/auditProcess.vue';

import request from '@/utils/request.js';
export default {
	components: {
		addDialog,
		otherList,
		incomeInfo,
		contractInfo,
		stepsCreate,
		divider,
		stepsLook,
		footerActionBtn,
		auditProcess
	},
	data() {
		const currentDate = this.getDate({
			format: true
		});
		return {
			confirm_id: 0,
			order_id: 0,
			isPreview: false,

			companyList: [],
			paymentList: [],

			// 添加项目弹窗
			showDialog: false,
			other_totals: 0,
			ritualList: [],
			orderParams: {
				remark: '',
				contract_img: [],
				receipt_img: [],
				note_img: []
			},
			add_type: [
				{
					id: 1,
					type: 'sugar',
					title: '喜糖'
				},
				{
					id: 2,
					type: 'wine',
					title: '酒水'
				},
				{
					id: 3,
					type: 'dessert',
					title: '点心'
				},
				{
					id: 4,
					type: 'led',
					title: 'LED'
				},
				{
					id: 5,
					type: 'light',
					title: '灯光'
				},
				{
					id: 6,
					type: '3d',
					title: '3D'
				},
				{
					id: 7,
					type: 'into',
					title: '进场费'
				},
				{
					id: 8,
					type: 'rite',
					title: '仪式'
				},
				{
					id: 9,
					type: 'room',
					title: '婚房'
				},
				{
					id: 10,
					type: 'part',
					title: '停车位'
				},
				{
					id: 11,
					type: 'other',
					title: '其他项目'
				}
			],

			banquetParams: {
				company_text: '',
				company_id: 0,
				sign_date: currentDate,
				banquet_totals: 0
			},

			addons: [],

			incomeParams: {
				receivable_no: '',
				banquet_income_payment: '',
				income_payment_text: '请选择',
				news_type: '12',
				banquet_income_type: 1,
				income_type_text: '请选择',
				banquet_income_item_price: '',
				banquet_income_date: '请选择'
			},

			confirmList: [],
			buttons: [], //是当前审核可以有的按钮
			confirm: {}, //审批信息

			transferForm: {
				realname: '',
				user_id: ''
			},
			sequenceData: [], // 新版审核
			confirmSteps: [], // 新版审核
				incomeType: [{
                   id: "5", title: "增项"
                }],
		};

	},
	computed: {
		startDate() {
			return this.getDate('start');
		},
		endDate() {
			return this.getDate('end');
		}
	},
	onLoad(options) {
		this.companyList = uni.getStorageSync('allPicker').companyList;
		this.paymentList = uni.getStorageSync('allPicker').paymentList;
		this.ritualList = uni.getStorageSync('allPicker').ritualList;
		// 添加 从订单详情页添加子项目 包含order_id
		if (options.params) {
			this.options = JSON.parse(options.params);
			this.order_id = this.options.order_id;
		}

		// 审核查看 lookApproval是个对象，包含的id是审核的编号，order_id要从审核详情中获取
		if (options.lookApproval) {
			this.options = JSON.parse(options.lookApproval);
			this.isPreview = true;
			//查看信息
			this.getDetail(this.options);
			this.confirm_id = this.options.id;
			// 审核流程--查看
			this.getConfirmList();
		}

		// 查看 从查看订单审核中进入只有id，这个id是审核的编号，order_id要从审核详情中获取
		if (options.id) {
			this.options = options;
			this.isPreview = true;
			//查看信息
			this.getDetail(this.options);
			this.confirm_id = this.options.id;
			// 审核流程--查看
			this.getConfirmList();
		}

		console.log(this.isPreview);
				this.$nextTick(()=> {
	 			this.$refs.income.getIncomeNature("fn",this.incomeType)
        	})
	},
	methods: {
		// 承办公司变化
		wedingCompanyChange(e) {
			let index = e.target.value;
			this.banquetParams.company_text = this.companyList[index].title;
			this.banquetParams.company_id = this.companyList[index].id;
			this.getConfirmSequence();
		},

		// 时间选择
		getDate(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();

			if (type === 'start') {
				year = year - 2;
			} else if (type === 'end') {
				year = year + 2;
			}
			month = month > 9 ? month : '0' + month;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		},

		//获取查看信息
		getDetail(e) {
			let that = this;
			request.request({
				url: '/h5/order.confirm/detail',
				showLoading: true,
				token: true,
				data: {
					id: e.id,
					type: e.user ? 'confirm_user' : ''
				},
				success(res) {
					if (res.code == 200) {
						that.banquetParams = res.data.detail.suborder;
						that.addons = res.data.detail.suborder.addons;
						that.incomeParams = res.data.detail.income;
						that.incomeParams.news_type = '12'; // 默认收款性质
						that.orderParams = res.data.detail.income;
						that.confirmList = res.data.confirmList;
						that.confirm = res.data.confirm; //代表的是当前审核的信息
						that.order_id = that.confirm.order_id;
						that.buttons = res.data.buttons ? res.data.buttons : [];
						that.additions = res.data.additionList;

						console.log(that.orderParams);
						//判断是否可以编辑
						that.buttons.map((item, index) => {
							if (item.id == 'update') {
								that.isPreview = false;
							}
						});
						that.companyList.map((item, index) => {
							if (item.id == that.banquetParams.company_id) {
								that.banquetParams.company_text = item.title;
							}
						});
						that.paymentList.map((item, index) => {
							if (item.id == that.incomeParams.banquet_income_payment) {
								that.incomeParams.income_payment_text = item.title;
							}
						});
					}
				},
				fail(e) {
					uni.showToast({
						icon: 'none',
						title: '服务器开小差去啦~'
					});
				}
			});
		},

		// 提交转交
		transferSubmit() {
			let that = this;
			sun.request({
				url: '/api/order.audit/transfer',
				showLoading: true,
				token: true,
				data: {
					id: that.confirm_id,
					user_id: that.transferForm.user_id
				},
				success(res) {
					if (res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
						uni.navigateBack({});
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				}
			});
		},
		// 点击取消时清除转交人员信息
		clearTransferForm() {
			this.transferForm = {
				realname: '',
				user_id: ''
			};
		},

		//查看详情审核流程
		getConfirmList() {
			let that = this;
			request.request({
				url: '/api/order.audit/getAuditResult',
				showLoading: true,
				token: true,
				data: { id: that.confirm_id },
				success(res) {
					that.confirmSteps = res.data.process;
				}
			});
		},
		//创建订单时的审核流程
		getConfirmSequence(e) {
			let that = this;
			request.request({
				url: '/api/order.audit/getAuditProcess',
				showLoading: true,
				token: true,
				data: Object.assign({ order_id: that.order_id }, { audit_type: 'suborder' }, { company_id: e.id }),
				success(res) {
					that.sequenceData = res.data.process;
				}
			});
		},

		// 添加其他售卖信息
		addOther() {
			this.showDialog = true;
		},

		// 关闭弹框
		closeDialog() {
			this.showDialog = false;
			this.isPreview = false;
		},

		// 删除已添加的其他费用
		deleteOtherItem(index) {
			let that = this;
			uni.showModal({
				title: '提示',
				content: '确定删除该项？',
				success(res) {
					if (res.confirm) {
						that.add_type.unshift(that.addons[index]);
						that.addons.splice(index, 1);
						that.getOtherPrice(that.addons);
					}
				}
			});
		},

		// 查看信息
		editOtherList(data) {
			this.isPreview = true;
			this.showDialog = true;
			this.$nextTick(function() {
				this.$refs['addDialog'].showDialog(data);
			});
		},

		// 获取其他费用
		getOtherPrice(data) {
			let otherPirce = 0;
			let len = data.length;
			for (let i = 0; i < len; i++) {
				otherPirce = otherPirce + parseFloat(data[i]['total']);
			}
			this.other_totals = otherPirce;
			this.getOrderTotals();
		},

		// 获取协议总额
		getOrderTotals() {
			let that = this;
			that.banquetParams.banquet_totals = parseFloat(that.other_totals).toFixed(2);
		},

		// 签约日期
		signDateChange(e) {
			this.banquetParams.sign_date = e.target.value;
		},

		// 提交转交
		transferSubmit() {
			let that = this;
			request.request({
				url: '/api/order.audit/transfer',
				showLoading: true,
				token: true,
				data: {
					id: that.confirm_id,
					user_id: that.transferForm.user_id
				},
				success(res) {
					if (res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
						uni.navigateBack({});
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				}
			});
		},

		// 点击取消时清除转交人员信息
		clearTransferForm() {
			this.transferForm = {
				realname: '',
				user_id: ''
			};
		},

		//查看详情审核流程
		getConfirmList() {
			let that = this;
			request.request({
				url: '/api/order.audit/getAuditResult',
				showLoading: true,
				token: true,
				data: {
					id: that.confirm_id
				},
				success(res) {
					that.confirmSteps = res.data.process;
				}
			});
		},
		//创建订单时的审核流程
		getConfirmSequence(e) {
			let that = this;
			request.request({
				url: '/api/order.audit/getAuditProcess',
				showLoading: true,
				token: true,
				data: Object.assign(
					{
						order_id: that.order_id
					},
					{
						audit_type: 'suborder'
					},
					{
						company_id: that.banquetParams.company_id
					}
				),
				success(res) {
					that.sequenceData = res.data.process;
				}
			});
		},

		// 编辑提交
		editFn(api, id) {
			let that = this;
			if (that.addons.length == 0) {
				uni.showToast({
					icon: 'none',
					title: '请选择增项信息'
				});
				return false;
			}

			let params = {};
			params['order_id'] = that.order_id;
			params['order_confirm_id'] = that.confirm_id;
			params['is_new'] = 1;
			params['suborder'] = that.banquetParams;
			params['addons'] = that.addons;
			params['income'] = Object.assign(that.incomeParams, that.orderParams);
			request.request({
				url: api,
				showLoading: true,
				token: true,
				data: params,
				success(res) {
					if (res.code == 200) {
						uni.redirectTo({
							url: '/pages/order/index'
						});
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				}
			});
		},

		//提交审核
		saveFn() {
			let that = this;
			if (that.addons.length == 0) {
				uni.showToast({
					icon: 'none',
					title: '请选择增项信息'
				});
				return false;
			}

			let params = {};
			params['order_id'] = that.order_id;
			params['is_new'] = 1;
			params['suborder'] = that.banquetParams;
			params['addons'] = that.addons;
			params['income'] = Object.assign(that.incomeParams, that.orderParams);
			request.request({
				url: that.options.api,
				showLoading: true,
				token: true,
				data: params,
				success(res) {
					if (res.code == 200) {
						uni.redirectTo({
							url: '/pages/order/index'
						});
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				}
			});
		},

		//底部评论按钮
		resubmits(item) {
			if (item.label == '同意' || item.label == '拒绝' || item.label == '评论') {
				this.commentFn(item);
			} else if (item.label == '转交') {
				uni.navigateTo({
					url: '../../../component/search/search?type=' + 'changeUser'
				});
			} else {
				this.editFn(item.api, item.id);
			}
		},
		// 编辑提交
		commentFn(e) {
			let that = this;
			let url = '';
			if (e.label == '拒绝') {
				url = '/api/order.audit/reject';
			} else if (e.label == '同意') {
				url = '/api/order.audit/accept';
			} else {
				url = e.api;
			}
			request.request({
				url: url,
				showLoading: true,
				token: true,
				data: {
					id: that.confirm_id,
					content: e.content ? e.content : '',
					image: e.image ? e.image : ''
				},
				success(res) {
					if (res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
						that.getDetail(that.options);
						that.getConfirmList();
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				}
			});
		}
	}
};
</script>

<style lang="less">
uni-page-body {
	padding-top: 0;
}

@import url('../../order/createOrder/components/common.less');
@import url('../../order/components/common.less');

.wedding-container {
	padding-bottom: 120upx;

	.footer-box {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 0 16px;
		display: flex;
		align-items: center;
		height: 100upx;
		font-size: 28upx;
		background-color: #ffffff;
		border-top: 1px solid #dedede;
		z-index: 9;

		.footer-title {
			flex: 0 15%;
		}

		.footer-action-btn {
			flex: 1;
			text-align: end;

			button {
				background-color: #007aff;
				color: #ffffff;
			}

			button + button {
				margin-left: 5px;
			}
		}
	}
}
</style>
